﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SIMS_GUI_Item
{
    public partial class AddAchievement : Form
    {
        SIMS_GUI_Item_DataBase db = new SIMS_GUI_Item_DataBase();
        public AddAchievement()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 传递参数
        /// </summary>
        /// <param name="RealName">姓名</param>
        /// <param name="UserName">帐号</param>
        public AddAchievement(string RealName,string UserName)
        {
            InitializeComponent();
            txt_RealName.Text = RealName;
            txt_UserName.Text = UserName;
        }

        SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-3CMC9MB\SQLEXPRESS;Initial Catalog=SIMS GUI Item DataBase;Integrated Security=True;");
        private void AddAchievement_Load(object sender, EventArgs e)
        {
            this.gradeTableAdapter.Fill(this.sIMS_GUI_Item_DataBaseDataSet9.Grade);
            var sql = "select * from dbo.Grade";
            SqlCommand comm = new SqlCommand(sql, conn);
            //创建数据适配器
            SqlDataAdapter adp = new SqlDataAdapter(sql, conn);
            //创建数据集
            DataSet ds = new DataSet();
            //向数据集填充数据
            adp.Fill(ds);

            this.cbo_Grade.DisplayMember = "GradeName"; //设置那一列给用户看
            this.cbo_Grade.ValueMember = "GradeId";     //设置关键数据列
            this.cbo_Grade.DataSource = ds.Tables[0];   //设置Combobox的数据源

            //是否允许自动创建列
            this.dgv_AddScore.AutoGenerateColumns = false;
            //不允许出现空行
            this.dgv_AddScore.AllowUserToAddRows = false;
            //整行选中
            this.dgv_AddScore.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //是否允许多选
            this.dgv_AddScore.MultiSelect = false;
            this.dgv_AddScore.RowTemplate.Height = 50;//更改datagridview的列高
        }


        private void btn_Add_Click(object sender, EventArgs e)
        {
            string GradeName = cbo_Grade.Text;
            string UserName = txt_UserName.Text;
            string CourseName = this.lbl_1.Text;
            string Score = this.txt_Score_1.Text;
            //实现循环变化科目以及dgv表中的成绩
            int number=0;
            for(int i = 0; i < 7; i++)
            {
                string labName = "lbl_" + (i+1);
                Control labcol = this.Controls.Find(labName,true)[0];
                Label lab = labcol as Label;
                string txtName = "txt_Score_" + (i + 1);
                Control txtcol = this.Controls.Find(txtName, true)[0];
                TextBox txt = txtcol as TextBox;
                int CourseId = 0;
                switch (lab.Text)
                {
                    case "计算机英语":
                        CourseId = 1;
                        break;
                    case "毛泽东思想概论":
                        CourseId = 3;
                        break;
                    case "体育":
                        CourseId = 4;
                        break;
                    case "数据库应用系统开发":
                        CourseId = 5;
                        break;
                    case "JavaScript程序设计":
                        CourseId = 6;
                        break;
                    case "SQLServer数据库管理和开发":
                        CourseId = 7;
                        break;
                    case "职业导向":
                        CourseId = 8;
                        break;
                    case "HTML+CSS":
                        CourseId = 9;
                        break;
                    case "HTML5+CSS3":
                        CourseId = 10;
                        break;
                    case "数学":
                        CourseId = 11;
                        break;
                    case "大学英语":
                        CourseId = 12;
                        break;
                    case "数据结构":
                        CourseId = 13;
                        break;
                    case "心理健康":
                        CourseId = 14;
                        break;
                    case "思想道德":
                        CourseId = 15;
                        break;
                    case "MVC架构":
                        CourseId = 16;
                        break;
                    case "JSQuery":
                        CourseId = 17;
                        break;
                    case "C++":
                        CourseId = 18;
                        break;
                    case "Phython":
                        CourseId = 19;
                        break;
                    case "Java":
                        CourseId = 20;
                        break;
                    case "软件测试":
                        CourseId = 21;
                        break;
                    case "PS":
                        CourseId = 22;
                        break;
                }
                if (!string.IsNullOrWhiteSpace(txt.Text))
                {
                    var sql = string.Format(@"update tab_Score set Score='{0}' where CourseId='{1}' and UserName='{2}'", Score, CourseId,UserName);
                    number = Convert.ToInt32(db.ExecNonQuery(sql));
                }
            }
            if (number > 0)
            {
                MessageBox.Show("更新成功");
                for(int i = 0; i < 7; i++)
                {
                    string txtName = "txt_Score_" +(i + 1);
                    Control txtcol = this.Controls.Find(txtName, true)[0];
                    TextBox txt = txtcol as TextBox;
                    txt.Clear();
                }
            }
            else
            {
                MessageBox.Show("更新失败");
            }
        }

        int courseGrade;
        private void cbo_Grade_SelectedIndexChanged(object sender, EventArgs e)
        {
            string UserName = txt_UserName.Text;
            string grade = this.cbo_Grade.Text;
            if (grade =="大一")
            {
                courseGrade = 1;
            }
            if (grade == "大二")
            {
                courseGrade = 2;
            }
            if (grade == "大三")
            {
                courseGrade=3;
            }
            string sql = string.Format(@"select a.CourseName,b.GradeName,c.Score from tab_Course a
                                         inner join Grade b on a.GradeId=b.GradeId
                                         inner join tab_Score c on a.CourseId=c.CourseId where UserName={0} and b.GradeId={1}",UserName, courseGrade);
            var dr = db.ExecReader(sql);//读取数据
            StringBuilder sb = new StringBuilder();

            SqlDataAdapter adp = new SqlDataAdapter(sql.ToString(), conn);//建立适配器
            DataSet ds = new DataSet();
            adp.Fill(ds);//填充数据
            this.dgv_AddScore.DataSource = ds.Tables[0];
            //读取科目数据
            int num = 0;
            while (dr.Read())
            {
                num++;
                for (int i = 0; i < 7; i++)
                {
                    if (num == 8)
                        num = 7;
                    var controlName = "lbl_" + num;
                    Control col = this.Controls.Find(controlName, true)[0];
                    Label lbl = col as Label;
                    lbl.Text = Convert.ToString(dr["CourseName"]);
                    for (int j = 0; j < 7; j++)
                    {
                        string txtName = "txt_Score_" + (j + 1);
                        Control txtcol = this.Controls.Find(txtName, true)[0];
                        TextBox txt = txtcol as TextBox;
                        txt.Clear();
                    }
                    break;
                }
            }
        }
    }
}
